$(function() {
		sildeImg(0);
		var SKU_Form='';
		var shop_info='';
		
		$(".add-cart").click(function(event) {
			var goods_id = $(this).data("goods-id");
			var image_url = $(this).data("image-url");
			var buy_enable = $(this).data("buy-enable");
			if(buy_enable==0){
				$.msg('商品库存不足！');
				return;
			}
			$.post('/cart/add', {
				goods_id: goods_id,
			}, function(result) {
				shop_info=result.data;
				var content='<div class="pop-choose-spec-mask" style="display: block;"></div>';
				//基本信息
				content +='<div class="pop-choose-spec-main" style="display: block;"><div class="pop-choose-spec-header"><img src="'+image_url+'" class="goods-thumb "><div class="attribute-header-right"><span class="goodprice choose-result-price"><em>￥'+shop_info.price+'</em></span><span class="goods-number">库存'+shop_info.num+'件</span></div>	<a class="pop-choose-spec-close layui-layer-close" href="javascript:void(0);" title="关闭"></a></div><div class="pop-choose-spec-con">';
				//sku属性 start
				content+='<div class="attr-list choose" style="overflow: hidden;">';
				//颜色
				if(shop_info.shop_sku){
					SKU_Form=shop_info.shop_sku.SKU_Form;
					if(shop_info.shop_sku.SKU_Style){
						var obj=shop_info.shop_sku.SKU_Style;
						var yanse='';
						$.each(obj, function(key, val) {
							if(key==0){
								input_yanse='<input type="hidden"  id="yanse" value="'+val.modifyValue+'" />';
								yanse+='<li class="selected" ><a href="javascript:void(0);" title="'+val.modifyValue+'"><span class="value-label">'+val.modifyValue+'</span></a><i></i></li>';
							}else{
								yanse+='<li><a href="javascript:void(0);" title="'+val.modifyValue+'"><span class="value-label">'+val.modifyValue+'</span></a></li>';
							}
						});
						content+='<dl class="attr"><dt class="dt">颜色：</dt><dd class="dd"><ul>'+input_yanse+yanse+'</ul></dd></dl>';
					}
					//规格
					if(shop_info.shop_sku.SKU_Spec){
						var obj=shop_info.shop_sku.SKU_Spec;
						var guige='';
						$.each(obj, function(key, val) {
							if(key==0){
								input_guige='<input type="hidden"  id="guige" value="'+val.modifyValue+'" />';
								guige+='<li class="selected" ><a href="javascript:void(0);" title="'+val.modifyValue+'"><span class="value-label">'+val.modifyValue+'</span></a><i></i></li>';
							}else{
								guige+='<li><a href="javascript:void(0);" title="'+val.modifyValue+'"><span class="value-label">'+val.modifyValue+'</span></a></li>';
							}
						});
							content+='<dl class="attr"><dt class="dt">规格：</dt><dd class="dd"><ul>'+input_guige+guige+'</ul></dd></dl>';
					}
				}
				
				content+='<div class="choose-btn"><dl class="amount"><dt class="dt">购买数量：</dt><dd class="dd"><span class="amount-widget">'+
				'<input type="number" class="amount-input" id="c_num" value="1" maxlength="8" title="请输入购买量" onkeyup="checkNum(this.value)"><span class="amount-btn">'+
				'<span class="amount-plus"><i>+</i></span><span class="amount-minus"><i>-</i></span></span><span class="amount-unit">件</span></span></dd></dl>'+
				'<input type="button" class="btn btn-primary btn-add-cart" value="加入购物车">'+
				'<input type="button" class="btn pop-choose-spec-close layui-layer-close" value="取消"></div></div></div>';
				layer.open({
				  type: 1,
				  title: false,
				  closeBtn: 0,
				  shadeClose: true,
				  skin: 'yourclass',
				  content: content
				});
			}, "json");
		});
		
		$("body").delegate(".amount-plus","click",function(){
			var t = $(".amount-input");
			t.val(parseInt(t.val())+1);  
        	$("#min").removeAttr("disabled");
		});
		$("body").delegate(".amount-minus","click",function(){
			var t = $(".amount-input");
			if (parseInt(t.val())>1) {                   
                t.val(parseInt(t.val())-1)  
            }else{  
                $("#min").attr("disabled","disabled")   
            } 
		});
		$("body").delegate(".attr dd ul li","click",function(){
			$(this).siblings('.selected i').remove();
			$(this).siblings().removeClass('selected');
			$(this).addClass('selected');
			$(this).append('<i></i>');
			var attr =$(this).find('span').text();
			$(this).siblings('input').val(attr);
			
			sku_price();
			
			
		});
		
		$("body").delegate(".btn-add-cart","click",function(event){
			var offset = $(".cart").offset(); 
			var id=shop_info.id;
			var yanse=$('#yanse').val();
			var guige=$('#guige').val();
			var num=$('#c_num').val();
			var num= parseInt(num);
			$.post('/cart/add_cart', {
				id: id,
				yanse: yanse,
				guige: guige,				
				num: num,				
			}, function(result) {
				if(result.code==1){
					$(".layui-layer-close").trigger("click");
					var img = $('.goods-thumb').attr('src'); 
					shop_fly(img,event);
					var count=$('.cart_num').html();
					var count= parseInt(count);
					var count= count+num;
					$('.cart_num').html(count);
					$('.sidebar-cartbox-goods-list').remove();
					get_cart_info();
					
				 }
			}, "json");
		});
		
		
});

function checkNum(num){
	$('.amount-input').val('');
	$('.amount-input').val(parseInt(num));
}

function toDecimal2(x) {    
        var f = parseFloat(x);    
        if (isNaN(f)) {    
            return false;    
        }    
        var f = Math.round(x*100)/100;    
        var s = f.toString();    
        var rs = s.indexOf('.');    
        if (rs < 0) {    
            rs = s.length;    
            s += '.';    
        }    
        while (s.length <= rs + 2) {    
            s += '0';    
        }    
        return s;    
}

function shop_fly(img,event){
	var offset = $('.sidebar-cartbox-trigger .cart').offset();  
  	var flyer = $('<img class="flyer-img" style="width:30px; height:30px" src="' + img + '">'); //抛物体对象   
  	
    flyer.fly({   
        start: {   
          	left: event.pageX - 20,
			top: event.pageY - $(window).scrollTop()  
        },   
        end: {   
           left: offset.left + 20,
			top: offset.top - $(window).scrollTop() + 50,
			width: 0,
			height: 0
        },   
        onEnd: function() {   
          $('.flyer-img').remove();
          $.msg('成功加入购物车！');
        }   
    });   
  
}
